package com.example.zhu.controller.commodity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhu.Mapper.*;
import com.example.zhu.VO.CommodityVO.CommodityVO;
import com.example.zhu.VO.RoomVo.RoomVO;
import com.example.zhu.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/commodity")
public class CommodityController {
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderProjectMapper orderProjectMapper;
    @PostMapping("/page")
    public Reply<CommodityVO> getAllList(@RequestBody Commodity commodity){
        String name = commodity.getName();
        MyPage page = commodity.getPage();
        page.setMin((page.getPage()-1)*page.getSize());
        page.setMax((page.getPage())*page.getSize());
        List<Commodity> CommodityAllList;
        if(name==null){
            CommodityAllList = commodityMapper.getAllList(page);
        }else{
           name="%"+name.trim()+"%";
           CommodityAllList = commodityMapper.getNameList(name);
        }
        CommodityVO commodityVO = new CommodityVO();
        commodityVO.setRecords(CommodityAllList);
        commodityVO.setSize(page.getSize()+"");
        commodityVO.setCurrent(page.getPage()+"");
        Integer allListCount = Integer.valueOf(commodityMapper.getAllListCount());
        commodityVO.setTotal(allListCount+"");
        commodityVO.setSearchCount(true);
        commodityVO.setPages(String.valueOf(allListCount/page.getSize()+1));
        return Reply.success(commodityVO);
    }
    @PostMapping("/modify")
    public Reply<Commodity>  modify(@RequestBody Commodity commodity){
        Commodity oldCommodity = commodityMapper.selectById(commodity);
        if (oldCommodity!=null){
            commodityMapper.updataById(commodity);
            return Reply.success(commodity);
        }else{
            // "修改失败"
            return Reply.failed(commodity);
        }
    }
    @PostMapping("/add")
    public Reply<Commodity>  add(@RequestBody Commodity commodity){
        Commodity oldCommodity = commodityMapper.selectByName(commodity);
        if (oldCommodity==null){
            commodityMapper.Insert(commodity);
            return Reply.success(commodity);
        }else{
            // "修改失败"
            return Reply.failed(commodity);
        }
    }
    @PostMapping("/delete/{id}")
    public Reply<Commodity>  delete(@PathVariable(name = "id") String id){
        Commodity commodity = new Commodity();
        commodity.setId(id);
        Commodity oldCommodity = commodityMapper.selectById(commodity);
        if (oldCommodity!=null){
            commodityMapper.Delect(commodity);
            return Reply.success(commodity);
        }else{
            // "修改失败"
            return Reply.failed(commodity);
        }
    }
    @PostMapping("/all")
    public Reply<List<Commodity>>  all(){
      List<Commodity> commoditys=  commodityMapper.selectAll();
        List<Employee> Employees =  employeeMapper.selectByStatus(1);
        for (Commodity commodity : commoditys) {
            commodity.setEmployees(Employees);
        }
        List<Commodity> updatedCommodities = commoditys.stream().peek(commodity -> commodity.setCount(0))
                .collect(Collectors.toList());
        return Reply.success(updatedCommodities);

    }
    @PostMapping("/OrderAll")
    public Reply<List<Commodity>>  OrderAll(@RequestBody RoomVO roomVO ){
        RoomVO room =  roomMapper.selectName(roomVO);
        String orderId = room.getOrderId();
        List<OrderProject> orderProjects= orderProjectMapper.selectList(orderId);
        List<Commodity> commoditys =  commodityMapper.selectAll();
        // 首先，将orderProjects转换为Map（键为name，值为count）
        Map<String,OrderProject > orderProjectCounts = orderProjects.stream()
                .collect(Collectors.toMap(OrderProject::getName, Function.identity()));
        List<Employee> Employees=  employeeMapper.selectByStatus(1);
        for (Commodity commodity : commoditys) {
            OrderProject orderProject = orderProjectCounts.get(commodity.getName());
            if (orderProject != null) {
                commodity.setCount(orderProject.getCount());
                commodity.setEmployeeType(orderProject.getEmployeeType());
                commodity.setEmployeeIng(orderProject.getNickname());
                Employee employee = employeeMapper.selectByNickName(orderProject.getNickname());
                Employees.add(employee);
            }
            commodity.setEmployees(Employees);
        }
        return Reply.success(commoditys);

    }
    @PostMapping("/OrderId")
    public Reply<List<Commodity>>  OrderId(@RequestBody RoomVO roomVO ){
        String orderId = roomVO.getOrderId();
        List<OrderProject> orderProjects= orderProjectMapper.selectList(orderId);
        List<Commodity> commoditys =  commodityMapper.selectAll();
        // 首先，将orderProjects转换为Map（键为name，值为count）
        Map<String,OrderProject > orderProjectCounts = orderProjects.stream()
                .collect(Collectors.toMap(OrderProject::getName, Function.identity()));
        List<Employee> Employees=  employeeMapper.selectByStatus(1);
        for (Commodity commodity : commoditys) {
            OrderProject orderProject = orderProjectCounts.get(commodity.getName());
            if (orderProject != null) {
                commodity.setCount(orderProject.getCount());
                commodity.setEmployeeType(orderProject.getEmployeeType());
                commodity.setEmployeeIng(orderProject.getNickname());
                Employee employee = employeeMapper.selectByNickName(orderProject.getNickname());
                Employees.add(employee);
            }
            commodity.setEmployees(Employees);
        }
        return Reply.success(commoditys);
        // 首先，将orderProjects转换为Map（键为name，值为count）

    }

}
